package edu.upc.wib.dao.impl;

import edu.upc.wib.dao.FavoritoDao;
import edu.upc.wib.entity.Contendor;
import edu.upc.wib.entity.Favorito;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Jhonny
 */
@Repository
public class FavoritoDaoImpl extends SimpleJdbcDaoSupport implements FavoritoDao {

    private static Logger log = LoggerFactory.getLogger(UsuarioDaoImpl.class);

    @Autowired
    public FavoritoDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }
    
    @Override
    public Integer crear(Favorito favorito) {
        // rol 1 es admin
        // rol 2 es igual a usuario normal
        getJdbcTemplate().update(
                "insert into favorito "
                + "("
                    + "idTorneo, "
                    + "idParticipante, "
                    + "contendor1, "
                    + "contendor2, "
                    + "ganador, "
                    + "posFixture"
                + ") "
                + "values (?, ?, ?, ?, ?, ?)",
                favorito.getIdTorneo(), 
                favorito.getIdParticipante(), 
                favorito.getContendor1().getIdContendor(), 
                favorito.getContendor2().getIdContendor(),
                favorito.getGanador(), 
                favorito.getPosFixture());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
    }
    
    @Override
    public List<Favorito> ListarPorTorneo(Integer idTorneo) {
        String sql = 
                "select f.idTorneo,"
                     + "f.idParticipante, "
                     + "c1.idContendor as idContendor1, "
                     + "c1.nombre as nombreContendor1, "
                     + "c1.texto as textoContendor1, "
                     + "c1.rutaImagen as rutaImagenContendor1, "
                     + "c1.agrupadorInicial as agrupadorInicialContendor1, "
                     + "c2.idContendor as idContendor2, "
                     + "c2.nombre as nombreContendor2, "
                     + "c2.texto as textoContendor2, "
                     + "c2.rutaImagen as rutaImagenContendor2, "
                     + "c2.agrupadorInicial as agrupadorInicialContendor2, "
                     + "f.ganador, "
                     + "f.posFixture "
              + "from favorito f "
                + "inner join contendor c1 on c1.idcontendor = f.contendor1 "
                    + "and c1.idtorneo = " + idTorneo + " "
                + "inner join contendor c2 on c2.idcontendor = f.contendor2 "
                    + "and c2.idtorneo = " + idTorneo + " "
              + "where f.idTorneo = " + idTorneo;
        
        List<Favorito> favoritos = new ArrayList<Favorito>();
        List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);
        
        for (Map row : rows) {
            Favorito favorito = new Favorito();
            
            favorito.setIdTorneo(idTorneo);
            favorito.setIdParticipante((Integer)row.get("idParticipante"));
            
            Contendor contendor = new Contendor
            (
                idTorneo, 
                (Integer)row.get("idContendor1"), 
                (String)row.get("nombreContendor1"), 
                (String)row.get("textoContendor1"), 
                (String)row.get("rutaImagenContendor1"),
                (Integer)row.get("agrupadorInicialContendor1")
            );
            favorito.setContendor1(contendor);
            
            contendor = new Contendor
            (
                idTorneo, 
                (Integer)row.get("idContendor2"), 
                (String)row.get("nombreContendor2"), 
                (String)row.get("textoContendor2"), 
                (String)row.get("rutaImagenContendor2"),
                (Integer)row.get("agrupadorInicialContendor2")
            );
            
            favorito.setContendor2(contendor);
            favorito.setGanador((Integer)row.get("ganador"));
            favorito.setPosFixture((Integer)row.get("posFixture"));
            
            favoritos.add(favorito);
	}
        
        return favoritos;
    }
}